/*************************************************************************
> File Name: client.c
> Author: High~兵 
> Mail: QQ:1398055460 
> Created Time: 2019年08月03日 星期六 11时50分27秒
************************************************************************/

#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/epoll.h>

int main(int argc , char * argv[])
{
    if(argc != 3)
    {
        printf("please input eg: ./a.out  server_port  server_ip\n");
        return -1;
    }	
    char buff[1024] = {0};
    while(1)
    {
        printf("input word-->:");
        scanf("%s",buff);
        if(!strcmp(buff,"exit"))
        {
            break;
        }
        int sockfd = socket(AF_INET,SOCK_STREAM,0);
        if(sockfd < 0)
        {
            perror("socket error");
            return -1;
        }
        struct sockaddr_in  destaddr;
        destaddr.sin_family         = AF_INET;
        destaddr.sin_port           = htons(atoi(argv[1]));
        destaddr.sin_addr.s_addr    = inet_addr(argv[2]);
        socklen_t len = sizeof(struct sockaddr_in);
        int ret ;
        ret = connect(sockfd,(struct sockaddr*)&destaddr,len);
        if(ret < 0)
        {
            perror("connrct error");
            return -1;
        }
        ret= send(sockfd,buff,sizeof(buff),0);//发送要查询的单词
        if(ret <= 0)
        {
            perror("sendto error");
            return -1;
        }
        bzero(buff,sizeof(buff));
        ret = recv(sockfd,buff,sizeof(buff),0);//接收单词注释
        printf("//:%s",buff);
        bzero(buff,sizeof(buff));
        close(sockfd);
    }
    return 0;
}

